<?php
/*
	$table
	$page
	$rowsPerPage
	
	$columns
	$editURL
	$deleteURL
	$parentURL				//url den trang chua view, can cho phan chia trang
	$checkboxURL			//url khi kich vao checkbox
	$enableEdit
	$enableDelete
	$orders
	$groupby
	$imageColumns
	$thumbnailColumns
	$comboColumns
	$editableColumns
*/
if(!isset($parentURL))
	$parentURL = 'admin/quanly/xem/' . $table ;
if(!isset($page))
	$page = 0;
$CI = &get_instance();
$CI->load->library('Table_viewer2');
$CI->load->database();

$query = $CI->db->query('show columns from ' . $table);
$checkboxColumns = array();
foreach($query->result() as $row)
{
	if(strpos($row->Type, 'tinyint') === 0)
	{
		if(isset($checkboxURL))
			$checkboxColumns[$row->Field] = $checkboxURL;
		else
			$checkboxColumns[$row->Field] = '';
	}
}

if(isset($rowsPerPage))
{
	$n = $CI->table_viewer2->db->count_all($table);
	if($page * $rowsPerPage >= $n)
		$page = (int)(max($n-1,0) / $rowsPerPage);
}

$CI->table_viewer2->db->from($table);
$CI->table_viewer2->m_table = $table;
if(isset($rowsPerPage))
	$CI->table_viewer2->db->limit($rowsPerPage, $page * $rowsPerPage);
if(isset($columns))
	$CI->table_viewer2->m_columns = $columns;
if(isset($editURL))
	$CI->table_viewer2->m_editURL = $editURL;
if(isset($deleteURL))
	$CI->table_viewer2->m_deleteURL = $deleteURL;
if(isset($enableEdit))
	$CI->table_viewer2->m_enableEdit = $enableEdit;
if(isset($enableDelete))
	$CI->table_viewer2->m_enableDelete = $enableDelete;
$CI->table_viewer2->m_param = $table . '/' . $page;
$CI->table_viewer2->m_checkboxColumns = $checkboxColumns;
if(isset($imageColumns))
{
	$CI->table_viewer2->m_imageColumns = array();
	foreach($imageColumns as $r)
	{
		$CI->table_viewer2->m_imageColumns[$r] = $r;
	}
}
if(isset($thumbnailColumns))
	$CI->table_viewer2->m_thumbnailColumns = $thumbnailColumns;
if(isset($comboColumns))
	$CI->table_viewer2->m_comboColumns = $comboColumns;
if(isset($editableColumns))
{
	$temp = array();
	foreach($editableColumns as $r)
	{
		$temp[$r] = $r;
	}
	$CI->table_viewer2->m_editableColumns = $temp;
}
if(isset($orders))
{
	foreach($orders as $key=>$value)
	{
		$CI->table_viewer2->db->order_by($key, $value);
	}
}
if(isset($groupby))
{
	$CI->table_viewer2->db->group_by($groupby);
}
if(isset($comboColumns))
{
	foreach($comboColumns as $key=>$value)
	{
		if(($CI->session->userdata('filter/' . $table . '/' . $key) != NULL) && ($CI->session->userdata('filter/' . $table . '/' . $key) != -1))
			$CI->table_viewer2->db->where($key, $CI->session->userdata('filter/'.$table.'/'.$key));
	}
}
if($CI->session->userdata('filter/chuoitimkiem'))
{
	if(strpos($CI->session->userdata('filter/chuoitimkiem'), '*') === FALSE)
	{
		$CI->table_viewer2->db->where("(sosim like '%" . $CI->session->userdata('filter/chuoitimkiem') . "%' or phanloai like '%" . $CI->session->userdata('filter/chuoitimkiem') . "%')");
	}
	else
	{
		$temp = str_replace('*', '%', $CI->session->userdata('filter/chuoitimkiem'));
		$CI->table_viewer2->db->where('sosim like \'' . $temp . "'");
	}
}
if($CI->session->userdata('filter/ngaynhaptu'))
{
	$ngay = date('d', $CI->session->userdata('filter/ngaynhaptu'));
	$thang = date('m', $CI->session->userdata('filter/ngaynhaptu'));
	$nam = date('Y', $CI->session->userdata('filter/ngaynhaptu'));
	$CI->table_viewer2->db->where("year(ngaynhap)>=$nam and month(ngaynhap)>=$thang and day(ngaynhap)>=$ngay");
	
	$ngay = date('d', $CI->session->userdata('filter/ngaynhapden'));
	$thang = date('m', $CI->session->userdata('filter/ngaynhapden'));
	$nam = date('Y', $CI->session->userdata('filter/ngaynhapden'));
	$CI->table_viewer2->db->where("year(ngaynhap)<=$nam and month(ngaynhap)<=$thang and day(ngaynhap)<=$ngay");
}

if($CI->session->userdata('filter/ngaybantu'))
{
	$ngay = date('d', $CI->session->userdata('filter/ngaybantu'));
	$thang = date('m', $CI->session->userdata('filter/ngaybantu'));
	$nam = date('Y', $CI->session->userdata('filter/ngaybantu'));
	$CI->table_viewer2->db->where("year(ngayban)>=$nam and month(ngayban)>=$thang and day(ngayban)>=$ngay");
	
	$ngay = date('d', $CI->session->userdata('filter/ngaybanden'));
	$thang = date('m', $CI->session->userdata('filter/ngaybanden'));
	$nam = date('Y', $CI->session->userdata('filter/ngaybanden'));
	$CI->table_viewer2->db->where("year(ngayban)<=$nam and month(ngayban)<=$thang and day(ngayban)<=$ngay");
}
if($CI->session->userdata('filter/trangthai') !== FALSE)
{
	$CI->table_viewer2->db->where('daban', $CI->session->userdata('filter/trangthai'));
}
//$CI->table_viewer2->m_columns = array('ID_danhmucsanpham'=> 'ID');
//$CI->load->view('admin/form', array('width'=>700, 'title'=>$table, 'subview'=>NULL, 'data'=>'<div align="center"><br>' . $CI->table_viewer2->show(TRUE) . '</div>'));
?>
<div align="center">
	<br />
<?php
echo $CI->table_viewer2->show(TRUE);
?>
<?php 
if(isset($rowsPerPage))
{
	$sotrang = (int)(max($n-1,0) / $rowsPerPage);
?>
</div>
<div align="center">
<table cellspacing="10">
<tr>
<?php
	if($page > 0)
	{
?>
<td>
<a href="<?=site_url($parentURL. "/0");?>">Đầu</a>
</td>
<?php
	}
?>
<?php
	if($page > 0)
	{
		$prev = $page - 1;
		echo '<td>';
		echo '<a href="' . site_url($parentURL . "/$prev") . '">Trước</a>';
		echo '</td>';
	}
	
	for($i=max(0, $page-5); $i<$page; $i++)
	{
		echo '<td>';
		echo '<a href="' . site_url($parentURL . "/$i") . '">' . ($i+1) . '</a>';
		echo '</td>';
	}
	echo '<td>';
	echo '<a>'.($page + 1).'</a>';
	echo '</td>';
	
	for ($i=$page+1; $i<min($page+5, $sotrang+1); $i++)
	{
		echo '<td>';
		echo '<a href="' . site_url($parentURL . "/$i") . '">' . ($i+1) . '</a>';
		echo '</td>';
	}
	
	if($page < $sotrang)
	{
		$next = $page + 1;
		echo '<td>';
		echo '<a href="' . site_url($parentURL . "/$next") . '">Sau</a>';
		echo '</td>';
	}
?>
<?php
if($page < $sotrang)
{
?>
<td>
<a href="<?=site_url($parentURL . "/$sotrang"); ?>">Cuối</a>
</td>
<?php
}
?>
</tr>
</table>
</div>
<?php
}
?>
<?php
if(isset($table))
	unset($table);
if(isset($page))
	unset($page);
if(isset($rowsPerPage))
	unset($rowsPerPage);
if(isset($columns))
	unset($columns);
if(isset($editURL))
	unset($editURL);
if(isset($deleteURL))
	unset($deleteURL);
?>